<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <div class="tatletitle">
      <el-row :gutter="20">
        <el-form ref="queryParams" :model="queryParams" :inline="true" label-width="70px">
          <el-form-item label="网站名称" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="部委" prop="general">
            <el-input
              v-model="queryParams.general"
              placeholder="请输入"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="地区" prop="city">
            <el-input
              v-model="queryParams.city"
              placeholder="请输入"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
            <!-- <el-cascader
              v-model="citys "
              :options="provinceCityOption"
              :show-all-levels="true"
              :props="{ value: 'id' }"
              filterable
              size="width:240px"
            />-->
          </el-form-item>
          <el-form-item label="行业" prop="industry">
            <el-input
              v-model="queryParams.industry"
              placeholder="请输入"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['library:policies:add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              v-hasPermi="['library:policies:remove']"
              type="primary"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除</el-button>
          </el-col>
        </el-row>
        <el-table
          stripe
          v-loading="loading"
          :data="dataList"
          @selection-change="handleSelectionChange"
          @sort-change="tableSortChange"
          :default-sort="{prop: 'createTime', order: 'descending'}"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column
            label="网站名称"
            align="center"
            prop="title"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column
            label="部委"
            align="center"
            prop="general"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column
            label="地区"
            align="center"
            prop="city"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
            width="100"
          />
          <el-table-column
            label="行业"
            align="center"
            prop="industry"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column label="链接" align="left">
            <template slot-scope="scope">
              <a :href="scope.row.url" target="_blank" style="color:#409EFF">{{ scope.row.url }}</a>
            </template>
          </el-table-column>
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            :sort-orders="['ascending', 'descending']"
            sortable="custom"
          />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['library:policies:edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >编辑</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>

      <!-- 添加或修改归档模板对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="720px">
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="网站名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入" />
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部委" prop="general">
                <el-input v-model="form.general" placeholder="请输入" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="地区" prop="city">
                <el-input v-model="form.city" placeholder="请输入" />
                <!-- <el-cascader
                  v-model="form.citys "
                  :options="provinceCityOption"
                  :show-all-levels="true"
                  :props="{ value: 'id' }"
                  filterable
                  size="width:240px"
                />-->
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="行业" prop="industry">
            <el-input v-model="form.industry" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="链接" prop="url">
            <el-input v-model="form.url" placeholder="请输入完整的URL地址链接，http://或https://开头" />
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="备注" type="textarea" :rows="2" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>
<script>
import aTitle from "@/components/Title";
import {
  policiesList,
  policiesInfo,
  delPolicies,
  addPolicies,
  updatePolicies
} from "@/api/library/policies.js";
import { provinceCityTwo } from "@/api/provinceCity";
export default {
  name: "RuleNavigation",
  components: {
    aTitle
  },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      // 遮罩层
      loading: false,
      // 选中数组
      guids: [],
      titles: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        general: undefined,
        industry: undefined,
        province: undefined,
        city: undefined,
        remark:undefined
      },
      citys: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [{ required: true, message: "名称不能为空", trigger: "blur" }]
        // attachmentId: [
        //   { required: true, message: "附件不能为空", trigger: "blur" }
        // ],
      },
      // 省市
      provinceCityOption: []
    };
  },
  created () {
    this.getList();
    provinceCityTwo().then(res => {
      this.provinceCityOption = res.data;
    });
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务标准库/法规导航.xlsx`
      )}&name=${encodeURIComponent("法规导航.xlsx")}`;
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.guids = selection.map(item => item.guid);
      this.titles = selection.map(item => item.title);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined
      };
      this.resetForm("form");
    },
    resetQuery () {
      this.queryParams.title = undefined;
      this.queryParams.general = undefined;
      this.queryParams.industry = undefined;
      this.queryParams.city = undefined;
      this.queryParams.remark = undefined;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加法规导航";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      policiesInfo({ guid: row.guid }).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改法规导航";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != undefined) {
            updatePolicies(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addPolicies(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = this.guids;
      const titles = row.title || this.titles;
      this.$confirm("是否确认删除网站名称为"+ titles +"的导航?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function () {
          return delPolicies(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () { });
    },
    /** 查询归档模板列表 */
    getList () {
      this.loading = true;
      // this.queryParams.province = this.citys[0]
      // this.queryParams.city = this.citys[1]
      policiesList(this.queryParams).then(response => {
        // this.dataList = response.rows.map(item => {
        //   item.address = item.province + item.city
        //   return item
        // })
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;

      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
};
</script>
<style lang="scss" scoped>
.tatletitle {
  margin: 0 20px;
}
/deep/.el-cascader {
  .el-input__inner {
    height: 32px !important;
    line-height: 32px !important;
  }
}
</style>
